﻿
//------------------------------------------------------------
// shaco Framework
// Copyright © 2017-2021 chang.liu All rights reserved.
// Feedback: 449612236@qq.com
//------------------------------------------------------------


using System.Collections.Generic;
using shaco.Base;
using UnityEngine;

namespace shaco
{
    public class DefaultResourcesHandle : IResourceHandle
    {
        public object assetObject => _assetObject;
        public float progress => _progress;
        public bool isDone => progress >= 1;
        public string assetPath => _assetPath;
        public object Current => this;

        System.Action<IResourceHandleBase> _onCompleted;
        Object _assetObject;
        float _progress = 0;
        string _assetPath = string.Empty;

        public void OnCompleted(System.Action<IResourceHandleBase> callback)
        {
            _onCompleted = callback;
        }

        public void Load<T>(string path) where T : Object
        {
            _assetPath = path;
            _assetObject = Resources.Load<T>(path);
            _progress = 1f;
            if (null != _onCompleted)
                _onCompleted(this);
        }

        public bool MoveNext()
        {
            return !isDone;
        }

        public void Reset()
        {
        }

        public void Release()
        {

        }
    }

    public class DefaultAllResourcesHandle : IAllResourceHandle
    {
        public IEnumerable<object> assetObjects => _assetObjects;
        public float progress => _progress;
        public bool isDone => progress >= 1;
        public System.Action<IResourceHandleBase> _onCompleted;
        public string assetPath => _assetPath;
        public object Current => this;

        Object[] _assetObjects;
        float _progress = 0;
        string _assetPath = string.Empty;

        public void OnCompleted(System.Action<IResourceHandleBase> callback)
        {
            _onCompleted = callback;
        }

        public void LoadAll<T>(string path) where T : Object
        {
            _assetPath = path;
            _assetObjects = Resources.LoadAll<T>(path);
            _progress = 1f;
            if (null != _onCompleted)
                _onCompleted(this);
        }

        public bool MoveNext()
        {
            return !isDone;
        }

        public void Reset()
        {
        }

        public void Release()
        {

        }
    }
}
